package com.mida.dms.knw.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mida.dms.knw.vo.CreateSqlVo;


public class DataSourceUtil {

    /**
     * 创建数据库连接
     * @param createSqlVo
     * @return
     * @throws Exception
     */
    public static DruidPooledConnection getConnection(CreateSqlVo createSqlVo) throws Exception {
        DruidPooledConnection conn = null;
        conn = createSqlDataSource(createSqlVo).getConnection();
        return conn;
    }

    /**
     * 初始化连接对象
     * @param createSqlVo
     * @return
     */
    private static DruidDataSource createSqlDataSource(CreateSqlVo createSqlVo) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(createSqlVo.getUrl().trim());
        dataSource.setDriverClassName(createSqlVo.getDriverClass().trim());
        dataSource.setUsername(createSqlVo.getUser().trim());
        dataSource.setPassword(createSqlVo.getPassword().trim());
        return dataSource;
    }
}
